<template>
	<view>
		<!-- 弹出授权框 -->
		<uni-popup ref="popup" background-color="#fff" :maskClick="false" @change="popchange">
			<view class="popup-content container">
				<view class="bg-white p9060">
					<view class="font_size_32 b-font color-black mb10">
						友步领队
					</view>
					<view class="font_size_32 b-font color-black mb50">
						申请获取并验证您的手机号提供完整功能服务
					</view>
					<view class="system-list-item-content tc mb50">
						<checkbox-group @change="changedFw">
							<label class="fwagreeDiv">
								<checkbox value="yes" style="transform:scale(0.7)" :checked="agreementfw=='yes'" />
							</label>
							已阅读并同意<text class="color-blue" @tap="navgetTo('pages/agreement/privacy')">《用户协议》</text>
							和<text class="color-blue" @tap="navgetTo('pages/agreement/agreement')">《隐私协议》</text>
						</checkbox-group>
					</view>
					<!-- <view class="mb30" v-show="registerUser">
						<button type="default" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">同意授权手机号</button>
					</view> -->
					<view class="mb30" v-show="!registerUser">
						<button type="default" :disabled="agreementfw!='yes'" @click="login">同意授权登录</button>
					</view>
					<view>
						<button type="default" @click="disagreeAuthorization">不同意授权</button>
					</view>
				</view>
			</view>
		</uni-popup>
		<!-- 新用户弹出注册框 -->
		<uni-popup ref="registerPopup" background-color="#fff" :maskClick="false" @change="registerPopchange">
			<view class="popup-content container">
				<view class="bg-white p7060">
					<view class="font_size_32 b-font color-black mb10">
						友步领队
					</view>
					<view class="font_size_32 b-font color-black mb50">
						申请获取并验证您的手机号提供完整功能服务
					</view>
					<view class="system-list-item-content tc mb50">
						<checkbox-group @change="changedFw">
							<label class="fwagreeDiv">
								<checkbox value="yes" style="transform:scale(0.7)" :checked="agreementfw=='yes'" />
							</label>
							已阅读并同意<text class="color-blue" @tap="navgetTo('pages/agreement/agreement')">《用户协议》</text>
							和<text class="color-blue" @tap="navgetTo('pages/agreement/agreement')">《隐私协议》</text>
						</checkbox-group>
					</view>
					<view class="mb30" v-show="registerUser">
						<button type="default" :disabled="agreementfw!='yes'" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">同意授权注册并登录</button>
					</view>
					<view>
						<button type="default" @click="disagreeAuthorization">不同意授权</button>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import api from '@/common/api/webapi.js';
	import storageUtil from '@/common/util/storageUtil.js';
	export default {
		name: "login-wx",
		data() {
			return {
				agreementfw: '',
				registerUser:false,
				isFirstLogin: true // 标记是否为首次登录
			};
		},
		methods: {
			login(){
				let _this = this;
				uni.login({
					provider: 'weixin',
					success: function(res) {
						if (res.code) {
							let params = {
								loginChannel: 'WECHAT',
								authCode: res.code, //
							}
							// 首次登录先请求接口判断是老用户还是新用户
							api.getWxUserInfo(params).then(res => {
								if (res.code === 0) {
									if(res.data.ifNewUesr=='T'){
										// 如果是新用户，直接打开注册弹窗
										_this.registerUser = true;
										storageUtil.saveStorageData("openid", res.data.openId);
										_this.$refs.popup.close();
										setTimeout(() => {
											_this.$refs.registerPopup.open("bottom");
										}, 300);
									}else{
										// 老用户正常登录流程
										storageUtil.saveStorageData("isLogin", true);
										uni.setStorageSync('token', res.data.token);
										_this.getUserInfo();
									}
								 
								} else {
									uni.showToast({
										icon: 'none',
										title: '登录失败！'
									});
								}
							}).catch()
						} else {
						}
					},
					fail: (res) => {
						_this.wxLogin()
					}
				})
			},
			getUserInfo(){
				let _this = this;
				api.getUserInfo().then(r => {
					if (r.code === 0) {
						if (r.data) {
							let saveData = r.data;
							storageUtil.saveUserData(saveData);
							// 登录成功后，重新加载页面数据
							setTimeout(() => {
							  // 方法1：直接调用父组件的checkIsLogin方法
							  if (_this.$parent && _this.$parent.checkIsLogin) {
							    _this.$parent.checkIsLogin();
							  }
							  
							  // 方法2：如果父组件没有响应，尝试通过页面栈刷新
							  const pages = getCurrentPages();
							  if (pages.length > 0) {
							    const currentPage = pages[pages.length - 1];
							    if (currentPage && currentPage.checkIsLogin) {
							      currentPage.checkIsLogin();
							    } else if (currentPage.onLoad) {
							      // 如果没有checkIsLogin方法，调用onShow方法
							      currentPage.onLoad();
							    }
							  }
							  
							  _this.close();
							}, 200);
						}
					}else{}
				}).catch()
			},
			//获取手机号登陆
			onGetPhoneNumber(e) {
				let _this = this;
				_this.close();
				if (e.detail.errMsg == "getPhoneNumber:fail user deny") { //用户决绝授权  
					//拒绝授权后弹出一些提示  
				} else { //允许授权 
					let openid = storageUtil.getStorageData('openid');
					let params = {
						loginChannel: 'WECHAT',
						authCode: e.detail.code, //
						openId: openid, //
					}
					api.RegisterWxUserInfo(params).then(res => {
						if (res.code === 0) {
							storageUtil.saveStorageData("isLogin", true);
							uni.setStorageSync('token', res.data.token);
							_this.getUserInfo();
							// 关闭注册弹窗和授权弹窗
							_this.$refs.registerPopup.close();
							_this.$refs.popup.close();
						} else {
							uni.showToast({
								icon: 'none',
								title: '登录失败！'
							});
						}
					}).catch()
				}
			},
			// 不同意授权处理函数
			disagreeAuthorization() {
				// 关闭所有弹窗
				this.$refs.popup.close();
				this.$refs.registerPopup.close();
				// 显示提示信息
				uni.showToast({
					icon: 'none',
					title: '您已拒绝授权，部分功能可能无法使用',
					duration: 2000
				});
			},
			open() {
				this.$refs.popup.open("bottom");
			},
			close() {
				this.$refs.popup.close();
			},
			navgetTo(page) {
				uni.navigateTo({
					url: "/" + page
				});
			},
			changedFw(e) {
				if (e.detail.value[0] != undefined)
					this.agreementfw = e.detail.value[0];
				else
					this.agreementfw = "";

			},
			popchange(e) {
				
			},
			registerPopchange(e) {
				
			}
		}

	}
</script>
<style lang="scss" scoped>
// 提高登录弹框的层级，确保它能覆盖页面上的所有元素
::v-deep .uni-popup {
  z-index: 9999 !important;
}

// 同时提高遮罩层的层级
::v-deep .uni-popup__mask {
  z-index: 9998 !important;
}

// 确保弹框内容区域的层级也正确
.popup-content {
  z-index: 9999 !important;
}
</style>